package cn.stylefeng.guns.sys.modular.projectPublicity.controller;

import cn.stylefeng.guns.base.auth.context.LoginContext;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.projectPublicity.entity.MainInfo;
import cn.stylefeng.guns.sys.modular.projectPublicity.mapper.MainInfoMapper;
import cn.stylefeng.guns.sys.modular.projectPublicity.model.params.MainInfoParam;
import cn.stylefeng.guns.sys.modular.projectPublicity.service.MainInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 公示表控制器
 *
 * @author WangZhen
 * @Date 2021-01-28 15:40:11
 */
@Controller
@RequestMapping("/mainInfo")
public class MainInfoController extends BaseController {

    private String PREFIX = "/modular/projectPublicity/mainInfo";

    @Autowired
    private MainInfoService mainInfoService;

    @Value("${web.upload-path-linux}")
    private String uploadPathLinux;
    @Value("${web.upload-path-windows}")
    private String uploadPathWindows;

    private String TRUE_PATH;

    /**
     * 项目图片
     */
    private static final String XMTP = "_xmtp";
    /**
     * 大会决议图片
     */
    private static final String JYTP = "_jytp";

    @Autowired
    private MainInfoMapper mainInfoMapper;

    /**
     * 跳转到主页面
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/mainInfo.html";
    }

    /**
     * 新增页面
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/mainInfo_add.html";
    }

    /**
     * 编辑页面
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/mainInfo_edit.html";
    }

    /**
     * 方案公示页面
     *
     * @return
     */
    @RequestMapping("/chooseUser")
    public String publicity() {
        return PREFIX + "/choose_user.html";
    }


    /**
     * 新增接口
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(@RequestParam Map<String, Object> param) {
        param.put("_session_user", LoginContextHolder.getContext().getUser().getId());
        mainInfoMapper.insertProjectPublicity(param);
        Object id = param.get("id");
        param.put("mainId", id);
        List houseList = JSON.parseArray((String) param.get("houseList"));
        for (Object obj : houseList) {
            param.put("_thisHouseId", obj);
            //插入人员
            mainInfoMapper.insertPublicityAndHouse(param);
        }

        boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
        if (isWindows) {
            TRUE_PATH = uploadPathWindows;
        } else {
            TRUE_PATH = uploadPathLinux;
        }

        try {
            File xmtp = new File(TRUE_PATH + id.toString() + XMTP);
            PrintStream ps = new PrintStream(new FileOutputStream(xmtp));
            File jytp = new File(TRUE_PATH + id.toString() + JYTP);
            PrintStream ps_jy = new PrintStream(new FileOutputStream(jytp));
            if (param.get("fileList") != null) {
                ps.println(param.get("fileList"));
            }
            if (param.get("file_list_jytp") != null) {
                ps_jy.println(param.get("file_list_jytp"));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            System.out.println("file ok: " + id);
        }
        return ResponseData.success();
    }


    /**
     * 编辑接口
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MainInfoParam mainInfoParam) {
        this.mainInfoService.update(mainInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(MainInfoParam mainInfoParam) {
        this.mainInfoService.delete(mainInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(MainInfoParam mainInfoParam) {
        MainInfo detail = this.mainInfoService.getById(mainInfoParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查看公示详情信息
     *
     * @author WangZhen
     * @Date 2021-02-05
     */
    @RequestMapping("/publicityInfo")
    @ResponseBody
    public ResponseData publicityInfo(@RequestParam Map<String, Object> param) {
        Map res = new HashMap();
        param.put("_session_user", LoginContextHolder.getContext().getUserId());
        List<Map<String, Object>> publicityInfo = this.mainInfoMapper.queryPublicityInfo(param);
        res.put("publicityInfo", publicityInfo);

        String path = "";
        boolean isWindows = System.getProperty("os.name").toLowerCase().contains("windows");
        if (isWindows) {
            TRUE_PATH = uploadPathWindows;
        } else {
            TRUE_PATH = uploadPathLinux;
        }
        path = TRUE_PATH + publicityInfo.get(0).get("id").toString() + XMTP;
        File file = new File(path);
        if (!file.exists() || !file.canRead()) {
            res.put("fileList", "");
        } else {
            BufferedReader reader = null;
            StringBuffer sbf = new StringBuffer();
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempStr;
                while ((tempStr = reader.readLine()) != null) {
                    sbf.append(tempStr);
                }
                reader.close();

                res.put("fileList", sbf.toString());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            res.put("fileList", sbf.toString());
        }

        res.put("publicityHouseList", this.mainInfoMapper.queryPublicityHouseList(param));


        return ResponseData.success(res);
    }

    /**
     * 查询列表
     *
     * @author WangZhen
     * @Date 2021-01-28
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(MainInfoParam mainInfoParam) {
        return this.mainInfoService.findPageBySpec(mainInfoParam);
    }

}


